import 'package:get/get.dart';
import 'package:webview_flutter/webview_flutter.dart';

class HLWebViewLogic extends GetxController {
  RxDouble progressValue = RxDouble(0.0);
  late WebViewController controller;

  /*
  * 初始化webView
  * */
  initWebViewController(String url) {
    controller = WebViewController()
      ..setJavaScriptMode(JavaScriptMode.unrestricted)
      ..setNavigationDelegate(
        NavigationDelegate(
          onProgress: (int progress) {
            progressValue.value = progress / 100.0;
          },
          onNavigationRequest: (NavigationRequest request) {
            return NavigationDecision.navigate;
          },
        ),
      )
      ..loadRequest(Uri.parse(url));
  }

  /*
  * 返回按钮
  * */
  getBack() async {
    if (await controller.canGoBack()) {
      controller.goBack();
    } else {
      Get.back();
    }
  }
}
